package gui.catalogues.products;

import gui.commons.CommonLayoutOperations;
import gui.commons.IdText;
import gui.commons.ToolbarListener;
import gui.messages.Messages;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;

/*
 *	2013.03.01 - Alejandro Alcaide.
 * 	ProductFormPanel should be considered a View Module of the application since it
 * 	displays the objects that interact with the user.
 * 
 * */

public class ProductFormPanel extends JPanel {	
	
	/*
	 *	Variables declaration
	 *
	 * */
	private ToolbarListener sl;
	private Border innerBorder = BorderFactory.createTitledBorder(Messages.getString("ProductFormPanel.PanelTitle")),  //$NON-NLS-1$
		outerBorder = BorderFactory.createEmptyBorder(15, 15, 15, 15); 
	private Border compoundBorder = BorderFactory.createCompoundBorder(
		outerBorder, innerBorder);
	Dimension dim = getPreferredSize();
	//private JLabel name, surname;
	private JTextField tfProductCode, tfProductDescription;
	private JButton ok;
	private GridBagConstraints gc;
	private int y;
	private Insets lblInset, tfInset, btnInset;
	private ProductFormListener fl;
	
	/*
	 * Repetitive tasks class variable
	 */
	private CommonLayoutOperations cOps;
	
	/*
	 *	ProductFormPanel variables
	 *
	 * */
	//private JList jlType;
	private DefaultComboBoxModel<IdText> dlmProductType;
	private JComboBox<IdText> jcbProductType;
	private JCheckBox jchFacturado;
	private JRadioButton rbTaxable, rbUnTaxable;
	private ButtonGroup bgRadios;
	
	public ProductFormPanel() {
		
		/*
		 *	Initializers and population
		 * 
		 * */
		initAndPopulate();
		
		/*
		 *	Generate the panel's layout
		 * 
		 * */
		generatePanelLayout();
		
		/*
		 * ActionListeners
		 */
		generateActionListeners();
		
		
	}
	
	public void generateActionListeners() {
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/*
				 *	The JComboBox 'jcbProductType' is casted as an IdText object 
				 *	to pass both id and string values
				 * */
				ProductFormEvent fe = new ProductFormEvent(
					e, tfProductCode.getText(), 
					tfProductDescription.getText(), 
					(IdText)jcbProductType.getSelectedItem(),
					bgRadios.getSelection().getActionCommand()
				);
				if(fl != null) {
					fl.ProductFormEventOccured(fe);
				}
			}
		});
	}
	
	public void initAndPopulate() {
		/*
		 * layout variables
		 * */
		gc = new GridBagConstraints();
		lblInset = new Insets(0, 0, 0, 1);
		tfInset = new Insets(0, 1, 0, 1);
		btnInset = new Insets(1, 1, 1, 1);
		cOps = new CommonLayoutOperations(lblInset, tfInset, btnInset);
		
		/*
		 * field variables
		 * */
		tfProductCode = new JTextField(2);
		tfProductDescription = new JTextField(6);
		ok = new JButton(Messages.getString("ProductFormPanel.ok")); //$NON-NLS-1$
		
		
		/*
		 *	ProductFormPanel initializers
		 *
		 * */
		jcbProductType = new JComboBox();
		dlmProductType = new DefaultComboBoxModel();
		rbTaxable = new JRadioButton(Messages.getString("ProductFormPanel.yes")); //$NON-NLS-1$
		rbUnTaxable = new JRadioButton(Messages.getString("ProductFormPanel.no")); //$NON-NLS-1$
		bgRadios = new ButtonGroup();
		
		/*
		 * Adding, setting actionCommand (a string that will
		 * tell which button is being pressed). 
		 */
		bgRadios.add(rbTaxable);
		bgRadios.add(rbUnTaxable);
		rbTaxable.setSelected(true);
		rbTaxable.setActionCommand("yes"); //$NON-NLS-1$
		rbUnTaxable.setActionCommand("no"); //$NON-NLS-1$
		
		/*	
		 * I populate the default model for my dlmProductType and set it in the latter
		 */
		dlmProductType.addElement(new IdText(0, Messages.getString("ProductFormPanel.cocktailDress"))); //$NON-NLS-1$
		dlmProductType.addElement(new IdText(1, Messages.getString("ProductFormPanel.nightGown"))); //$NON-NLS-1$
		dlmProductType.addElement(new IdText(2, Messages.getString("ProductFormPanel.earrings"))); //$NON-NLS-1$
		dlmProductType.addElement(new IdText(3, Messages.getString("ProductFormPanel.ring"))); //$NON-NLS-1$
		dlmProductType.addElement(new IdText(4, Messages.getString("ProductFormPanel.bracelet"))); //$NON-NLS-1$
		dlmProductType.addElement(new IdText(5, Messages.getString("ProductFormPanel.collar"))); //$NON-NLS-1$
		dlmProductType.addElement(new IdText(6, Messages.getString("ProductFormPanel.bag"))); //$NON-NLS-1$
		dlmProductType.addElement(new IdText(7, Messages.getString("ProductFormPanel.scarf"))); //$NON-NLS-1$
		dlmProductType.addElement(new IdText(8, Messages.getString("ProductFormPanel.cups"))); //$NON-NLS-1$
		dlmProductType.addElement(new IdText(8, Messages.getString("ProductFormPanel.shoes"))); //$NON-NLS-1$
		
		jcbProductType.setModel(dlmProductType);
	}
	
	public void generatePanelLayout() {
		/*
		 *	setting size and borders for the Panel
		 *
		 */
		dim.height = 350;
		setPreferredSize(dim);
		
		setBorder(compoundBorder);
		
		/*
		 *	Manage Layout
		 * 
		 * */
		setLayout(new GridBagLayout());
		//	position x, y
		gc.gridx = 0;
		gc.gridy = 0;
		//	padding (minimum space left between component and enclosing cell
		gc.ipadx = 5;
		gc.ipady = 3;
		//	space relative to other cells around
		gc.weightx = 0.5;
		gc.weighty = 0.1;
		//	Take or not the space left in the layout
		gc.fill = GridBagConstraints.NONE;
		
		/*
		 *	Populate the grid with its components
		 * 
		 * */
		y = 0;
		cOps.addJLabel(this, Messages.getString("ProductFormPanel.code"), 0, y); //$NON-NLS-1$
		cOps.addTextField(this, tfProductCode, 1, y);
		y++;	// 1
		cOps.addJLabel(this, Messages.getString("ProductFormPanel.description"), 0, y); //$NON-NLS-1$
		cOps.addTextField(this, tfProductDescription, 1, y);
		y++;	// 2
		cOps.addJLabel(this, Messages.getString("ProductFormPanel.productTypes"), 0, y); //$NON-NLS-1$
		cOps.addJComboBox(this, jcbProductType, true, 1, y);
		y++;	// 3
		cOps.addJLabel(this, Messages.getString("ProductFormPanel.taxable"), 0, y); //$NON-NLS-1$
		cOps.addJRadioButton(this, rbTaxable, 1, y);
		y++;	// 4
		cOps.addJRadioButton(this, rbUnTaxable, 1, y);
		y++;	// last
		cOps.addJButton(this, ok, 2, y);
	}
	
	//	method to assign a textPanel to the formPanel so the latter will be listened
	public void setStringListener(ToolbarListener sl) {
		this.sl = sl;
	}
	
	/*
	 *	Object listener setter for other classes to get access to this panel
	 * 
	 * */
	public void setProductFormListener(ProductFormListener fl) {
		this.fl = fl;
	}
	
//	public void addLabel(String lbl, int x, int y) {
//		gc.anchor = GridBagConstraints.LINE_END;
//		gc.gridx = x;
//		gc.gridy = y;
//		gc.ipadx = 5;
//		gc.ipady = 1;
//		gc.weighty = 0.1;
//		gc.insets = lblInset;
//		add(new JLabel(lbl), gc);
//	}
//	
//	public void addTextField(JTextField t, int x, int y) {
//		gc.anchor = GridBagConstraints.LINE_START;
//		t.setMinimumSize(t.getPreferredSize());
//		gc.gridx = x;
//		gc.gridy = y;
//		gc.ipadx = 110;
//		gc.ipady = 1;
//		gc.weighty = 1;
//		gc.insets = tfInset;
//		add(t, gc);
//	}
//	
//	public void addJRadioButtong(JRadioButton rb, int x, int y) {
//		gc.anchor = GridBagConstraints.LINE_START;
//		gc.gridx = x;
//		gc.gridy = y;
//		gc.ipadx = 1;
//		gc.ipady = 0;
//		gc.weighty = 0.01; 
//		// I'm using same insets as the textfields'
//		gc.insets = tfInset;
//		add(rb, gc);
//	}
//	
//	public void addJList(JList l, int x, int y) {
//		gc.anchor = GridBagConstraints.LINE_START;
//		l.setMinimumSize(l.getPreferredSize());
//		l.setBorder(BorderFactory.createEtchedBorder());
//		l.setSelectedIndex(0);
//		gc.gridx = x;
//		gc.gridy = y;
//		gc.ipadx = 110;
//		gc.ipady = 1;
//		gc.weighty = 1;
//		//	using same inset as textfields
//		gc.insets = tfInset;
//		add(l, gc);
//	}
//	
//	public void addJComboBox(JComboBox jcb, int x, int y) {
//		gc.anchor = GridBagConstraints.LINE_START;
//		jcb.setMinimumSize(jcb.getPreferredSize());
//		jcb.setBorder(BorderFactory.createEtchedBorder());
//		jcb.setSelectedIndex(0);
//		gc.gridx = x;
//		gc.gridy = y;
//		gc.ipadx = 110;
//		gc.ipady = 1;
//		gc.weighty = 1;
//		//	using same inset as textfields
//		gc.insets = tfInset;
//		add(jcb, gc);
//	}
//	
//	public void addButton(JButton b, int x, int y) {
//		gc.anchor = GridBagConstraints.LINE_START;
//		gc.gridx = x;
//		gc.gridy = y;
//		gc.ipadx = 2;
//		gc.ipady = 5;
//		gc.weighty = 5;
//		gc.insets = btnInset;
//		add(b, gc);
//	}
}
